package com.jiangxue.service;

/**
 * Created by Administrator on 2015/12/16.
 */

import org.apache.lucene.index.IndexWriter;
import java.io.File;
import com.jiangxue.common.utils.wordToDocument;


/**
 * IndexInitBean
 *
 * @author: zhangteng
 * @time: 2014/12/14 13:32
 */
public class creatIndex {

    private IndexWriter indexWriter;

    private File dataDir;

    public IndexWriter getIndexWriter() {
        return indexWriter;
    }

    public void setIndexWriter(IndexWriter indexWriter) {
        this.indexWriter = indexWriter;
    }

    public File getDataDir() {
        return dataDir;
    }

    public void setDataDir(File dataDir) {
        this.dataDir = dataDir;
    }

    public void doIndex() throws Exception {
        if (!dataDir.isDirectory()) {
            return ;
        }
        File[] dataFiles = dataDir.listFiles();
        int dataFileNumbers = dataFiles.length;
        for (int i = 0;i < dataFileNumbers; ++i) {
            if (dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".doc")) {
                indexWriter.addDocument(wordToDocument.getDocument(dataFiles[i]));
                indexWriter.commit();
            }
        }
        indexWriter.close();
    }
}
